import { DataMgr } from "../Data/DataMgr";
import { playerData } from "../Data/playerData";
import { playerExpData } from "../configData/playerExpData";
import { worldData } from "../Data/worldData";
import { ifPosition, ifPlayerData, ifWorldData, ifEnemyData, ifIdOpr } from "../Data/dataBase";

export class netService extends cc.Component{

    private static _instance:netService = new netService();
    static Ins()
    {
        return this._instance;
    }

    private _playerData:playerData;
    private _worldData:worldData;

    init(){
        this._playerData = DataMgr.Ins().getModel("PLAYER_DATA");
        this._worldData = DataMgr.Ins().getModel("WORLD_DATA");
        this.schedule(this.enemyGen.bind(this), 5);
    }

    private enemyGen(){
        for(var i = 0; i<=10;i++){
            var data:ifEnemyData = {
                hp:100,
                maxHp:100,
                pos:{x:0,y:-1000},
            }
            this._worldData.ADD_ENEMY(data);
        }
    }

    private playerLevelUp(amount:number){
        this._playerData.LEVEL(amount);
        this._playerData.MAX_HP(amount * 100);
        this._playerData.HP(this._playerData.data.maxHp);
        this._playerData.NEXT_EXP(playerExpData[this._playerData.data.level+1]);
    }

    private addExp(amount:number){
        this._playerData.EXP_OPR(amount);
        var level;
        for(var i in playerExpData){
            if(this._playerData.data.exp < playerExpData[i])
            {
                cc.log(this._playerData.data.exp);
                cc.log(playerExpData[i]);
                level = parseInt(i);
                break;
            }
            if(parseInt(i) == (playerExpData.length-1)){
                level = playerExpData.length;
            }
        }
        if(level && this._playerData.data.level != level)
            this.playerLevelUp(level);
    }

    private addEnemy(data:ifEnemyData){
        this._worldData.ADD_ENEMY(data);
    }

    private addPlayer(data:ifPlayerData){
        this._worldData.ADD_PLAYER(data);
    }

    private delEnemy(id:number){
        this._worldData.DEL_ENEMY(id);
        this._worldData.KILL_COUNT_OPR(1);
    }

    private delPlayer(id:number){
        this._worldData.DEL_PLAYER(id);
    }

    public getPlayerData() {
        var playerData:ifPlayerData = {
            hp:100,
            maxHp:100,
            level:1,
            pos:{x:0, y:0},
            exp:0,
        }
        this._playerData.DATA(playerData);
        this._playerData.NEXT_EXP(playerExpData[this._playerData.data.level+1]);
    }

    public getWorldData(){
        var worldData:ifWorldData = {
            enemyList: [],
            playerList: [],
            killCount: 0,
        }
        this._worldData.Data(worldData);
    }

    public updatePosition(pos:ifPosition){
        this._playerData.POSITION(pos);
    }

    public attackEnemy(data:ifIdOpr){
        this._worldData.ENEMY_HP_OPR(data);
        if(this._worldData.data.enemyList[data.id].hp <= 0){
            this.delEnemy(data.id);
            this.addExp(50);
        }
    }
}

